package com.smt.take.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.smt.take.R
import com.smt.take.model.beans.GoodsDetailsBean
import com.smt.take.ui.activity.BusinessActivity
import com.smt.take.ui.fragment.GoodsFragment
import com.smt.take.utils.Constants
import com.smt.take.utils.PriceFormater
import com.smt.take.utils.TakeOutApp

class CartRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val goodsFragment: GoodsFragment =
        (context as BusinessActivity).fragments[0] as GoodsFragment
    private var cartList: MutableList<GoodsDetailsBean> = mutableListOf()

    fun setCartListData(cartList: MutableList<GoodsDetailsBean>) {
        this.cartList = cartList
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_cart, parent, false)
        return CartViewHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as CartViewHolder).bindData(cartList[position])
    }

    override fun getItemCount(): Int {
        return cartList.size
    }

    inner class CartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
        View.OnClickListener {

        private lateinit var goodsInfo: GoodsDetailsBean
        private val tvName: TextView = itemView.findViewById(R.id.tv_name)
        private val tvAllPrice: TextView = itemView.findViewById(R.id.tv_type_all_price)
        private val tvCount: TextView = itemView.findViewById(R.id.tv_count)
        private val ibAdd: ImageButton = itemView.findViewById(R.id.ib_add)
        private val ibMinus: ImageButton = itemView.findViewById(R.id.ib_minus)

        init {
            ibAdd.setOnClickListener(this)
            ibMinus.setOnClickListener(this)
        }

        override fun onClick(v: View?) {
            var isAdd = false
            when (v?.id) {
                R.id.ib_add -> {
                    isAdd = true
                    doAddOperation()
                }
                R.id.ib_minus -> doMinusOperation()
            }
            //左侧红点
            processRedDotCount(isAdd)
            // 下方购物栏刷新
            (goodsFragment.activity as BusinessActivity).updateCartUI()
        }

        // 红点处理
        private fun processRedDotCount(isAdd: Boolean) {
            // 找到此商品属于的类别
            val typeId = goodsInfo.typeId
            // 找到此类别在左侧列表中的位置
            val typePosition = goodsFragment.presenter.getTypePositionByTypeId(typeId)
            // 最后找到tvRedDotCount
            val goodsTypeInfo = goodsFragment.presenter.goodsTypeList[typePosition]
            var redDotCount = goodsTypeInfo.redDotCount
            if (isAdd) {
                redDotCount++
            } else {
                redDotCount--
            }
            goodsTypeInfo.redDotCount = redDotCount
            // 刷新左侧列表
            goodsFragment.goodsTypeAdapter.notifyDataSetChanged()
        }

        // 加号处理
        private fun doAddOperation() {
            //数据层count
            var count = goodsInfo.count
            // 更新缓存
            TakeOutApp.instance.updateCacheSelectedInfo(goodsInfo.id, Constants.ADD)
            count++
            goodsInfo.count = count
            //购物车内部数量与价格
            notifyDataSetChanged()
            //右侧列表
            goodsFragment.goodsAdapter.notifyDataSetChanged()
        }

        // 减号处理
        private fun doMinusOperation() {
            //数据层count
            var count = goodsInfo.count
            if (count == 1) {
                // 数量为1，再减需要移除此条目
                cartList.remove(goodsInfo)

                // 最后一个类别移除后，关闭购物车
                if (cartList.size == 0) {
                    (context as BusinessActivity).showOrHideCart()
                }

                // 删除缓存
                TakeOutApp.instance.deleteCacheSelectedInfo(goodsInfo.id)
            } else {
                // 更新缓存
                TakeOutApp.instance.updateCacheSelectedInfo(goodsInfo.id, Constants.MINUS)
            }
            count--
            goodsInfo.count = count
            //购物车内部数量与价格
            notifyDataSetChanged()
            //右侧列表
            goodsFragment.goodsAdapter.notifyDataSetChanged()
        }

        fun bindData(goodsInfo: GoodsDetailsBean) {
            this.goodsInfo = goodsInfo
            tvName.text = goodsInfo.name
            tvAllPrice.text = PriceFormater.format(goodsInfo.newPrice.toFloat() * goodsInfo.count)
            tvCount.text = goodsInfo.count.toString()
        }

    }
}